home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / 3D Class Library / 3dcl.h < prev    next >
Text File  |  1996-06-27  |  5KB  |  251 lines

  1. // =======================================================================
  2. //    3D Class Library, © Xilex Group
  3. // - ------------------------------------------------------------------- -
  4. //    Written by Dmitry Boldyrev
  5. // =======================================================================
  6.  
  7. #pragma once
  8.  
  9. #include "types.h"
  10. #include "cosin.h"
  11. #include <math.h>
  12.  
  13. #include "LinkList.h"
  14.  
  15. typedef WORD matrix3d[3][3];
  16.  
  17. typedef struct 
  18. {
  19.     WORD     x;                    //    x coordinate in space
  20.     WORD    y;                    //    y coordinate in space
  21.     WORD    z;                    //    z coordinate in space
  22. } vector3d;
  23.  
  24. typedef struct
  25. {
  26.     WORD     x;                    //    x projected coordinate
  27.     WORD    y;                    //    y projected coordinate
  28.     WORD    c;                    //    color/intensity of the edge
  29.     WORD    u;                    //    x texture coordinate
  30.     WORD    v;                    //    y texture coordinate
  31. } upoint;
  32.  
  33. enum {
  34.     isVertex,                    //    vertex is a VERTEX
  35.     isNormal                    //    vertex is a normal
  36. };
  37.  
  38. class vertex : public ListNode
  39. {
  40.     public:
  41.         vector3d    xyz;        // xyz coordinate in space
  42.         upoint        xy;            // xy coordinate on the projected plane
  43.         vector3d    normal;        // average normal to the vertex
  44.         WORD         nCount;        // how many normals were added to compute the average
  45.         
  46.         WORD        mKind;        // isVertex or isNormal?
  47.         
  48.         // =======================================================================
  49.         //    Constructors
  50.         // =======================================================================
  51.         
  52.         vertex();
  53.         vertex(WORD x, WORD y, WORD z, WORD inKind);
  54.  
  55.         void setTo(WORD x, WORD y, WORD z, WORD inKind);
  56.     
  57.         void xform2d();
  58.         void setNewOrigin(vertex *p);
  59.         void globalXform2origin(vertex *p);
  60.         void copyOrigin(vertex *p);
  61.     
  62.         // =======================================================================
  63.         //    Rotations/Translations stuff
  64.         // =======================================================================
  65.         
  66.         void localRotate(matrix3d mat);
  67.         void translate(WORD dX, WORD dY, WORD dZ);
  68.     
  69.         // =======================================================================
  70.         //    Normal computations
  71.         // =======================================================================
  72.         
  73.         void zeroNormal();
  74.         void addNormal(WORD dX, WORD dY, WORD dZ);
  75.         void avgNormal();
  76.     
  77.         virtual ~vertex();
  78.     
  79.     protected:
  80. };
  81.  
  82. WORD dotProduct(const vector3d *p1, const vector3d *p2);
  83.  
  84. typedef struct
  85. {
  86.     WORD p1, p2, p3, color;
  87. } polyRec;
  88.  
  89. enum {
  90.     sNone = 0,
  91.     sWire,
  92.     sFlat,
  93.     sGouraud,
  94.     sTexMap,
  95.     sGouraudTexMap
  96. };
  97.  
  98. enum {
  99.     tx32x32,
  100.     tx64x64,
  101.     tx128x128,
  102.     tx256x256,
  103.     tx512x512
  104. };
  105.  
  106. class polygon : public ListNode
  107. {
  108.     public:
  109.     
  110.         vertex *p1, *p2, *p3, normal;
  111.     
  112.         polygon();
  113.         polygon(vertex *v1, vertex *v2, vertex *v3);
  114.  
  115.         void calcThetaPhi(vector3d v, WORD &theta, WORD &phi);
  116.  
  117.         void setTo(vertex *v1, vertex *v2, vertex *v3);
  118.     
  119.         WORD calcAvgZ();
  120.     
  121.         void setNewOrigin(vertex *p);
  122.         
  123.         void localRotate(matrix3d mat);
  124.         void globalRotate(matrix3d mat);
  125.  
  126.         void setGNormals();
  127.     
  128.         void renderGouraudTexMap();
  129.         void renderTexMap();
  130.         void renderGouraud();
  131.         void renderWire();
  132.         void renderFlat();
  133.         void renderNone();
  134.  
  135.         void renderPoly(BYTE method);
  136.         void applyTexture(WORD width, WORD height, WORD depth);
  137.         
  138.            ~polygon();
  139.  
  140.         WORD    averageZ;
  141.     protected:
  142. };
  143.  
  144. typedef struct
  145. {
  146.     WORD numPoints, numPolys;
  147. } objFileHeader;
  148.  
  149. class object3d : public ListNode
  150. {
  151.     public:
  152.     
  153.         object3d();
  154.  
  155.         void save(char *fname);
  156.         void load(char *fname);
  157.         void importV3D(char *fname);
  158.         void importASC(char *fname);
  159.  
  160.         void addLocalPoint(WORD x, WORD y, WORD z, WORD inKind);
  161.         void addLocalPoint(vertex *p);
  162.             
  163.         void addLocalPoly(WORD p1, WORD p2, WORD p3);
  164.         void addLocalPoly(polygon *pg);
  165.     
  166.         WORD getPointNum(vertex *p);
  167.         vertex *getPointAddr(WORD pNum);
  168.     
  169.         void localRotate();
  170.         void makeRotMatrix(WORD alpha, WORD theta, WORD phi);
  171.     
  172.         void translate(WORD dX, WORD dY, WORD dZ);
  173.         void setLocation(WORD x, WORD y, WORD z);
  174.     
  175.         void sortPlanes();    
  176.         
  177.         void zeroGNormals();
  178.         void setGNormals();
  179.         void display();
  180.  
  181.         void applyTexture();
  182.         void calcBounds(WORD &width, WORD &height, WORD &depth);    
  183.         virtual ~object3d();
  184.     
  185.         LinkList points;
  186.         LinkList polys;
  187.         WORD      count, xDeg, yDeg, zDeg;
  188.     
  189.     protected:
  190.         Stack         stack1[256], stack2[256];
  191.         matrix3d      mat;
  192. };
  193.  
  194. class world3d
  195. {
  196.     public:
  197.     
  198.         world3d();
  199.         virtual ~world3d();
  200.         void globalRotate(WORD tX, WORD tY, WORD tZ);
  201.         void insertObject(object3d *object);
  202.         void deleteObject(object3d *object, BOOL doDisposeIt = TRUE);
  203.         void draw();
  204.  
  205.     protected:
  206.     
  207.         LinkList objects;
  208.         WORD      xDeg, yDeg, zDeg;
  209. };
  210. class viewPoint
  211. {
  212.  
  213. public:
  214.  
  215.     vector3d location, light, view;
  216.  
  217.     viewPoint();
  218.  
  219.     void rotate(int tX, int tY, int tZ);
  220.     void translate(int dX, int dY, int dZ);
  221.  
  222.     ~viewPoint();
  223.  
  224. private:
  225.  
  226.  
  227. };
  228.  
  229. #define ABS(x) (((x) < 0) ? -(x) : (x))
  230.  
  231. extern world3d world;
  232. extern viewPoint camera;
  233.  
  234. extern WORD matrix[9];
  235. extern WORD *perspect;
  236.  
  237. extern Point middle;
  238. extern Ptr baseAddr;
  239. extern short rowBytes;
  240.  
  241. #define    SQR(x)    ((x) * (x))
  242. #define    SQRT(x)    (sqrt(x))
  243.  
  244. extern WORD startx[480], endx[480], startcol[480], endcol[480];
  245.  
  246. void ASM_GouraudPoly(upoint p1, upoint p2, upoint p3);
  247. void C_GouraudPoly(upoint p1, upoint p2, upoint p3);
  248. void C_TexturePoly(upoint p1, upoint p2, upoint p3);
  249.  
  250.  
  251.